package com.lanou.bbs.service;

import com.lanou.bbs.dao.UserDao;
import com.lanou.bbs.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public User login(User user) throws SQLException {
        //调用dao,通过username查询数据库
        User u = userDao.findByUsername(user.getUsername());
        //u!null:查到了,u得到的密码跟 user的密码相等,确认登录成功
        if (u != null && u.getPassword().equals((user.getPassword()))) {
            return u;
        } else {
            //登录失败
            return null;
        }

    }

    public boolean register(User user) {
        User u = null;
        try {
            u = userDao.findByUsername(user.getUsername());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (u != null) {
            return false;
        } else {
            try {
                userDao.add(user);
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }
}
